package cn.tedu.cwfw.dynamic.service.impl;

import cn.tedu.cwfw.comment.mapper.CommentMapper;
import cn.tedu.cwfw.comment.pojo.vo.CommentVO;
import cn.tedu.cwfw.dynamic.mapper.DynamicMapper;
import cn.tedu.cwfw.dynamic.pojo.dto.DynamicQuery;
import cn.tedu.cwfw.dynamic.pojo.dto.SaveDynamicFrom;
import cn.tedu.cwfw.dynamic.pojo.vo.DynamicVO;
import cn.tedu.cwfw.dynamic.pojo.entity.Dynamic;
import cn.tedu.cwfw.dynamic.service.DynamicService;
import cn.tedu.cwfw.feedback.pojo.entity.Feedback;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class DynamicServiceImpl implements DynamicService {
    @Autowired
    private DynamicMapper dynamicMapper;

    @Autowired
    private CommentMapper commentMapper;

    /**
     * 根据类型获取动态
     * @param dynamicQuery 动态类型
     * @return List<DynamicVO>
     */
    @Override
    public List<DynamicVO> selectByType(DynamicQuery dynamicQuery) {
        log.debug("获取首页动态类型请求业务:入参dynamicQuery={}",dynamicQuery);
        List<DynamicVO> list = new ArrayList<>();
        if ("10".equals(dynamicQuery.getType())){
            list = dynamicMapper.dynamicsFollow(dynamicQuery.getClientId());
        } else if ("20".equals(dynamicQuery.getType())) {
            list = dynamicMapper.dynamicsRecommend();
        }
        log.debug("获取首页动态类型响应业务:入参:{},出参list={}",dynamicQuery,list);
        // 获取评论内容
        for (DynamicVO dynamicVO : list){
            dynamicVO.setComments(getCommentList(dynamicVO.getId()));
        }
        return list;
    }

    @Override
    public void delete(Long id) {
        log.debug("删除动态请求业务:id={}",id);
        dynamicMapper.deleteDynamic(id);
        commentMapper.deleteCommentByDynamicId(id);
    }

    /**
     * 根据id获取动态
     * @param id 动态id
     * @return DynamicVO
     */
    @Override
    public DynamicVO selectDynamicById(Long id) {
        log.debug("获取单个动态请求业务:id={}",id);
        DynamicVO dynamicVO = dynamicMapper.selectDynamicById(id);
        dynamicVO.setComments(getCommentList(dynamicVO.getId()));
        log.debug("获取单个动态响应业务:id={},出参dynamicVO={}",id,dynamicVO);

        // 更新浏览数
        Dynamic dynamic = new Dynamic();
        BeanUtils.copyProperties(dynamicVO,dynamic);
        dynamic.setViewCount(dynamicVO.getViewCount()+1);
        dynamicMapper.updateDynamic(dynamic);
        return dynamicVO;
    }

    /**
     * 点赞
     * @param id 动态id
     */
    @Override
    public void likeDynamic(Long id) {
        log.debug("点赞请求业务:id={}",id);
        DynamicVO dynamicVO = dynamicMapper.selectDynamicById(id);
        Dynamic dynamic = new Dynamic();
        dynamic.setId(dynamicVO.getId());
        // 更新点赞数
        dynamic.setLikeCount(dynamicVO.getLikeCount()+1);
        dynamicMapper.updateDynamic(dynamic);
    }

    /**
     * 保存动态/修改动态
     * @param saveDynamicFrom 动态信息
     */
    @Override
    public void saveDynamic(SaveDynamicFrom saveDynamicFrom) {
        log.debug("保存动态请求业务:saveDynamicFrom={}",saveDynamicFrom);
        Dynamic dynamic = new Dynamic();
        BeanUtils.copyProperties(saveDynamicFrom,dynamic);
        if (dynamic.getId()!=null){
            dynamicMapper.updateDynamic(dynamic);
        }else{
            dynamic.setCreateTime(new Date());
            dynamicMapper.addDynamic(dynamic);
        }
    }

    /**
     * 获取评论
     * @param id dynamicId
     * @return List<CommentVO>
     */
    private List<CommentVO> getCommentList(Long id){
        log.debug("获取评论请求业务:id={}",id);
        List<CommentVO> list = commentMapper.selectCommentByDynamicId(id);
        log.debug("获取评论响应业务:id={},出参list={}",id,list);
        return list;
    }
}
